package com.example.musicplayer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.util.EncodingUtils;

import android.R.bool;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class sdcardsevice {
	private Context context;

	public sdcardsevice() {

	}

	public sdcardsevice(Context context) {
		this.context = context;
	}


	boolean saveContentToSd(String childDrectory, String fileName,
			String content) {
		boolean flag = false;
		FileOutputStream fileOutputStream = null;
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			File f = new File(Environment.getExternalStorageDirectory()
					.toString() + childDrectory);
			if (!f.exists()) {
				f.mkdirs();
			}
			File file = new File(Environment.getExternalStorageDirectory()
					.toString() + childDrectory, fileName);
			try {
				fileOutputStream = new FileOutputStream(file);
				byte[] buffer = content.getBytes();
				try {
					fileOutputStream.write(buffer);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (fileOutputStream != null) {
					try {
						fileOutputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			return true;
		}
		return flag;
	}

	public boolean deleteFile(String path) {
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			File file = new File(path);
			if (file.exists()) {
				return file.delete();
			}
		}
		return false;
	}

	String readFile(String childDrectory, String filename) {
		FileInputStream fileInputStream = null;
		String temp = "";
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			File f = new File(Environment.getExternalStorageDirectory()
					.toString() + childDrectory);
			if (!f.exists()) {
				f.mkdirs();
			}
			File file = new File(Environment.getExternalStorageDirectory()
					.toString() + childDrectory, filename);
			if (file.exists()) {
				try {
					fileInputStream = new FileInputStream(file);
					int len = 0;
					try {
						len = fileInputStream.available();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					byte[] buffer = new byte[len];
					try {
						fileInputStream.read(buffer);
						temp = EncodingUtils.getString(buffer, "utf-8");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					try {
						if (fileInputStream != null) {
							fileInputStream.close();
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else {
				try {
					FileOutputStream fileOutputStream = new FileOutputStream(
							file);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return temp;
	}
}
